package com.eduction.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eduction.bean.SessionUser;
import com.eduction.common.ReturnCode;
import com.eduction.modules.system.entity.Expert;
import com.eduction.resource.BaseController;
import com.eduction.service.ObservationService;

@Controller
@RequestMapping("/Observation")
public class ObservationController extends BaseController{

	private static final Logger log = Logger.getLogger(ObservationController.class);
	
	@Autowired
	private ObservationService observationService;
	
	/**
	 * 查询专家信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryAllIndexTarget" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryExpertInfo(HttpServletRequest request) {
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}
		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(expertId ==null)
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		Expert e = observationService.queryExpertInfo(expertId);
		if(null == e)
			return toResponse(null, ReturnCode.FAIL, "该专家不存在!");
		return toResponse(e, ReturnCode.SUCCESS, "查询成功!");
	}
	
	/**
	 * 修改专家信息
	 */
	@RequestMapping(value = "/updataExpertInfo" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> updataExpertInfo(HttpServletRequest request,@RequestBody Map<String ,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}
		String expertId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(expertId ==null)
			return toResponse(null, ReturnCode.FAIL, "未登录!");
		String s_name = map.get("name");//专家名
		String s_expert_code = map.get("code");//专家编号
		String s_title = map.get("title");//职称
		String s_post = map.get("post");//职务
		String s_field = map.get("field");//研究领域
		String s_officephone = map.get("officephone");//办公电话
		String s_phone = map.get("phone");//手机
		String s_email = map.get("email");//邮箱
		String s_workunit = map.get("workunit");//工作单位
		
		if(null == s_name ||null ==s_expert_code || StringUtils.isBlank(s_name) || StringUtils.isBlank(s_expert_code)){
			return toResponse(null, ReturnCode.FAIL, "编号,姓名不能为空");
		}
		Expert e = observationService.queryExpertInfo(expertId);
		if(null == e)
			return toResponse(null, ReturnCode.FAIL, "该专家不存在!");
		e.setS_name(s_name);
		e.setS_expert_code(s_expert_code);
		e.setS_title(s_title);
		e.setS_post(s_post);
		e.setS_field(s_field);
		e.setS_officephone(s_officephone);
		e.setS_phone(s_phone);
		e.setS_email(s_email);
		e.setS_workunit(s_workunit);
		observationService.saveExpertInfo(e);
		return toResponse(null, ReturnCode.SUCCESS, "保存成功!");
	}
}
